﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Data.SqlClient;

namespace Smartdispatch
{
    public partial class WaterSmart : Form
    {
        private static Settings Setting = new Settings();
        private static ModelLive ModelLives = new ModelLive();
        private static bool Getcmd = false;

        public static bool Stepin = false;//单步执行开关

        private CommandOut resultout = new CommandOut();
        public WaterSmart()
        {
            InitializeComponent();
           
            var ima= Image.FromFile("backgroup.gif");
           
            this.BackgroundImage = ima;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            Setting.Visible = false;
            ModelLives.Visible = false;
            Timer timer = new Timer();
            timer.Interval = 10000;
            timer.Tick += Timer_Tick;
            timer.Start();
            TreeShow treeShow = new TreeShow();
            treeShow.Visible = true;
            panel1.Visible = false;
            textBox1.Click += TextBox1_Click;
            textBox2.Click += TextBox2_Click;
            this.FormClosing += WaterSmart_FormClosing;
           // this.ShowInTaskbar = false;
        }

        private void WaterSmart_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            
           // throw new NotImplementedException();
        }

        private void TextBox2_Click(object sender, EventArgs e)
        {
            if(!panel1.Visible)
            textBox2_TextChanged(sender,e);
        }

        private void TextBox1_Click(object sender, EventArgs e)
        {
            textBox1_TextChanged(sender,e);
        }

        /// <summary>
        /// 定时执行生成指令
        /// 勾选自动执行才执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer_Tick(object sender, EventArgs e)
        {
            if(Getcmd)
            button3_Click(sender, e);
            //throw new NotImplementedException();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Setting.Visible = true;
            Setting.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ModelLives.Visible = true;
        }

        private void WaterSmart_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 自动执行模式为更新指令
        /// 非自动为根据仿真界面数据生成指令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string cntstr = $@"Data Source=192.168.0.86;Initial Catalog=Tabwater_dev_new;User ID=sa;Password=abc123!@#";
            string sqlstr = $@"update K_FlowChart set Run=0 ";
            try
            {
                SqlConnection sqlcnt = new SqlConnection(cntstr);
                sqlcnt.Open();
                SqlCommand sqlCommand = new SqlCommand(sqlstr, sqlcnt);
                sqlCommand.ExecuteNonQuery();
                sqlcnt.Close();
            }
            catch
            {
               // MessageBox.Show("");
            }
            TagNameEntity.Step = TagNameEntity.Step + 1;

            CtrlFun ctrlfun = new CtrlFun();
            ctrlfun.tazishan.RideState = new bool[4];
            ctrlfun.fengHuang.RideState = new bool[10];
            ctrlfun.hongHe.RideState = new bool[4];
            ctrlfun.waterFactory_2.RideState = new bool[10];
            ctrlfun.waterFactory_5.RideState = new bool[11];
            #region 读取配置
            //方桥一期
            ctrlfun.fangQiao1.AutoCtrl =! Setting.checkBox8.Checked;
            ctrlfun.fangQiao1.ACtrl = Setting.checkBox17.Checked;
            ctrlfun.fangQiao1.BCtrl = Setting.checkBox16.Checked;
            //方桥二期
            ctrlfun.fangQiao2.AutoCtrl = !Setting.checkBox9.Checked;
            ctrlfun.fangQiao1.ACtrl = Setting.checkBox19.Checked;
            ctrlfun.fangQiao1.BCtrl = Setting.checkBox18.Checked;
            //光华
            ctrlfun.guangHua.AutoCtrl = !Setting.checkBox5.Checked;
            ctrlfun.guangHua.ACtrl = Setting.checkBox10.Checked;
            ctrlfun.guangHua.BCtrl = Setting.checkBox11.Checked;
            //清波
            ctrlfun.qingBo.AutoCtrl = !Setting.checkBox6.Checked;
            ctrlfun.qingBo.ACtrl = Setting.checkBox13.Checked;
            ctrlfun.qingBo.BCtrl = Setting.checkBox12.Checked;
            //南北
            ctrlfun.nanBei.AutoCtrl = !Setting.checkBox7.Checked;
            ctrlfun.nanBei.ACtrl = Setting.checkBox15.Checked;
            ctrlfun.nanBei.BCtrl = Setting.checkBox14.Checked;
            //土门
            ctrlfun.tuMen.AutoCtrl = !Setting.checkBox1.Checked;
            ctrlfun.tuMen.ACtrl = Setting.checkBox21.Checked;
            ctrlfun.tuMen.BCtrl = Setting.checkBox20.Checked;
            //土桥
            ctrlfun.tuQiao.AutoCtrl = !Setting.checkBox2.Checked;
            ctrlfun.tuQiao.ACtrl = Setting.checkBox23.Checked;
            ctrlfun.tuQiao.BCtrl = Setting.checkBox22.Checked;
            //青杠
            ctrlfun.qiaoGang.AutoCtrl = !Setting.checkBox3.Checked;
            ctrlfun.qiaoGang.ACtrl = Setting.checkBox25.Checked;
            ctrlfun.qiaoGang.BCtrl = Setting.checkBox24.Checked;
            //郎家
            ctrlfun.langJia.AutoCtrl = !Setting.checkBox4.Checked;
            ctrlfun.langJia.ACtrl = Setting.checkBox27.Checked;
            ctrlfun.langJia.BCtrl = Setting.checkBox26.Checked;
            //凤凰加压站
            ctrlfun.fengHuang.TargetPoolMax = float.Parse(Setting.textBox20.Text);
            ctrlfun.fengHuang.TargetPoolMin = float.Parse(Setting.textBox19.Text);
            ctrlfun.fengHuang.Canopenridenum = 5;
            //洪河加压站
            ctrlfun.hongHe.TargetPoolMax = float.Parse(Setting.textBox22.Text);
            ctrlfun.hongHe.TargetPoolMin = float.Parse(Setting.textBox21.Text);
            ctrlfun.hongHe.Canopenridenum = 4;
            //塔子山加压站
            ctrlfun.tazishan.TargetPoolMax = float.Parse(Setting.textBox24.Text);
            ctrlfun.tazishan.TargetPoolMin = float.Parse(Setting.textBox23.Text);
            ctrlfun.tazishan.Canopenridenum = 4;
            try
            {
                if (ModelLives.textBox11.Text.Trim() != "0")
                    ctrlfun.tazishan.Canopenridenum = 4;
                else
                    ctrlfun.tazishan.Canopenridenum = int.Parse(ModelLives.textBox11.Text);

                if (ModelLives.textBox12.Text.Trim() != "0")
                    ctrlfun.hongHe.Canopenridenum = 4;
                else
                    ctrlfun.hongHe.Canopenridenum = int.Parse(ModelLives.textBox12.Text);
                if (ModelLives.textBox17.Text.Trim() != "0")
                    ctrlfun.fengHuang.Canopenridenum = 4;
                else
                    ctrlfun.fengHuang.Canopenridenum = int.Parse(ModelLives.textBox17.Text);

            }
            catch
            {
                MessageBox.Show("可开启车台数输入格式不正确，请检查");
            }
            #endregion
            ctrlfun.waterFactory_2.Canopenridenum = 7;
            ctrlfun.waterFactory_5.Canopenridenum = 8;
            if(checkBox1.Checked)
            {
                ctrlfun.GetData();//自动读取，自动工作模式刷新指令
            }
            else
            {
                try
                {
                    ctrlfun.fangQiao1.Pressure_A = float.Parse(ModelLives.textBox47.Text);
                    ctrlfun.fangQiao1.Pressuer_B = float.Parse(ModelLives.textBox46.Text);
                    ctrlfun.fangQiao1.Open_A = float.Parse(ModelLives.textBox45.Text);
                    ctrlfun.fangQiao1.Open_B = float.Parse(ModelLives.textBox44.Text);
                    ctrlfun.fangQiao1.SubPressure = float.Parse(ModelLives.textBox43.Text);

                    ctrlfun.fangQiao2.Pressure_A = float.Parse(ModelLives.textBox52.Text);
                    ctrlfun.fangQiao2.Pressuer_B = float.Parse(ModelLives.textBox51.Text);
                    ctrlfun.fangQiao2.Open_A = float.Parse(ModelLives.textBox50.Text);
                    ctrlfun.fangQiao2.Open_B = float.Parse(ModelLives.textBox49.Text);
                    ctrlfun.fangQiao2.SubPressure = float.Parse(ModelLives.textBox48.Text);

                    ctrlfun.nanBei.Pressure_A = float.Parse(ModelLives.textBox42.Text);
                    ctrlfun.nanBei.Pressuer_B = float.Parse(ModelLives.textBox41.Text);
                    ctrlfun.nanBei.Open_A = float.Parse(ModelLives.textBox40.Text);
                    ctrlfun.nanBei.Open_B = float.Parse(ModelLives.textBox39.Text);
                    ctrlfun.nanBei.SubPressure = float.Parse(ModelLives.textBox38.Text);

                    ctrlfun.qingBo.Pressure_A = float.Parse(ModelLives.textBox57.Text);
                    ctrlfun.qingBo.Pressuer_B = float.Parse(ModelLives.textBox56.Text);
                    ctrlfun.qingBo.Open_A = float.Parse(ModelLives.textBox55.Text);
                    ctrlfun.qingBo.Open_B = float.Parse(ModelLives.textBox54.Text);
                    ctrlfun.qingBo.SubPressure = float.Parse(ModelLives.textBox53.Text);

                    ctrlfun.guangHua.Pressure_A = float.Parse(ModelLives.textBox62.Text);
                    ctrlfun.guangHua.Pressuer_B = float.Parse(ModelLives.textBox61.Text);
                    ctrlfun.guangHua.Open_A = float.Parse(ModelLives.textBox60.Text);
                    ctrlfun.guangHua.Open_B = float.Parse(ModelLives.textBox59.Text);
                    ctrlfun.guangHua.SubPressure = float.Parse(ModelLives.textBox58.Text);

                    ctrlfun.langJia.Pressure_A = float.Parse(ModelLives.textBox37.Text);
                    ctrlfun.langJia.Pressuer_B = float.Parse(ModelLives.textBox36.Text);
                    ctrlfun.langJia.Open_A = float.Parse(ModelLives.textBox35.Text);
                    ctrlfun.langJia.Open_B = float.Parse(ModelLives.textBox34.Text);
                    ctrlfun.langJia.SubPressure = float.Parse(ModelLives.textBox33.Text);

                    ctrlfun.qiaoGang.Pressure_A = float.Parse(ModelLives.textBox72.Text);
                    ctrlfun.qiaoGang.Pressuer_B = float.Parse(ModelLives.textBox71.Text);
                    ctrlfun.qiaoGang.Open_A = float.Parse(ModelLives.textBox70.Text);
                    ctrlfun.qiaoGang.Open_B = float.Parse(ModelLives.textBox69.Text);
                    ctrlfun.qiaoGang.SubPressure = float.Parse(ModelLives.textBox68.Text);

                    ctrlfun.tuMen.Pressure_A = float.Parse(ModelLives.textBox31.Text);
                    ctrlfun.tuMen.Pressuer_B = float.Parse(ModelLives.textBox30.Text);
                    ctrlfun.tuMen.Open_A = float.Parse(ModelLives.textBox29.Text);
                    ctrlfun.tuMen.Open_B = float.Parse(ModelLives.textBox28.Text);
                    ctrlfun.tuMen.SubPressure = float.Parse(ModelLives.textBox32.Text);

                    ctrlfun.tuQiao.Pressure_A = float.Parse(ModelLives.textBox67.Text);
                    ctrlfun.tuQiao.Pressuer_B = float.Parse(ModelLives.textBox66.Text);
                    ctrlfun.tuQiao.Open_A = float.Parse(ModelLives.textBox65.Text);
                    ctrlfun.tuQiao.Open_B = float.Parse(ModelLives.textBox64.Text);
                    ctrlfun.tuQiao.SubPressure = float.Parse(ModelLives.textBox63.Text);

                    ctrlfun.tazishan.Pool = float.Parse(ModelLives.textBox8.Text);
                    ctrlfun.tazishan.RealInSpeed = float.Parse(ModelLives.textBox10.Text);
                    ctrlfun.tazishan.RealOutSpeed = float.Parse(ModelLives.textBox7.Text);
                    ctrlfun.tazishan.Squat = float.Parse(ModelLives.textBox9.Text);
                    ctrlfun.tazishan.RideState[0] = ModelLives.checkBox1.Checked;
                    ctrlfun.tazishan.RideState[1] = ModelLives.checkBox2.Checked;
                    ctrlfun.tazishan.RideState[2] = ModelLives.checkBox3.Checked;
                    ctrlfun.tazishan.RideState[3] = ModelLives.checkBox4.Checked;

                    ctrlfun.fengHuang.Pool = float.Parse(ModelLives.textBox20.Text);
                    ctrlfun.fengHuang.RealInSpeed = float.Parse(ModelLives.textBox18.Text);
                    ctrlfun.fengHuang.RealOutSpeed = float.Parse(ModelLives.textBox21.Text);
                    ctrlfun.fengHuang.Squat = float.Parse(ModelLives.textBox19.Text);
                    ctrlfun.fengHuang.RideState[0] = ModelLives.checkBox12.Checked;
                    ctrlfun.fengHuang.RideState[1] = ModelLives.checkBox11.Checked;
                    ctrlfun.fengHuang.RideState[2] = ModelLives.checkBox10.Checked;
                    ctrlfun.fengHuang.RideState[3] = ModelLives.checkBox9.Checked;
                    ctrlfun.fengHuang.RideState[4] = ModelLives.checkBox16.Checked;
                    ctrlfun.fengHuang.RideState[5] = ModelLives.checkBox15.Checked;
                    ctrlfun.fengHuang.RideState[6] = ModelLives.checkBox14.Checked;
                    ctrlfun.fengHuang.RideState[7] = ModelLives.checkBox13.Checked;
                    ctrlfun.fengHuang.RideState[8] = ModelLives.checkBox17.Checked;
                    ctrlfun.fengHuang.RideState[9] = ModelLives.checkBox18.Checked;

                    ctrlfun.hongHe.Pool = float.Parse(ModelLives.textBox20.Text);
                    ctrlfun.hongHe.RealInSpeed = float.Parse(ModelLives.textBox18.Text);
                    ctrlfun.hongHe.RealOutSpeed = float.Parse(ModelLives.textBox16.Text);
                    ctrlfun.hongHe.Squat = float.Parse(ModelLives.textBox19.Text);
                    ctrlfun.hongHe.RideState[0] = ModelLives.checkBox8.Checked;
                    ctrlfun.hongHe.RideState[1] = ModelLives.checkBox7.Checked;
                    ctrlfun.hongHe.RideState[2] = ModelLives.checkBox6.Checked;
                    ctrlfun.hongHe.RideState[3] = ModelLives.checkBox5.Checked;
                   // MessageBox.Show(ModelLives.checkBox1.Text);
                    ctrlfun.waterFactory_6.RealSpeed = float.Parse(ModelLives.textBox1.Text);
                    ctrlfun.waterFactory_6.Pool3 = float.Parse(ModelLives.textBox3.Text);
                    ctrlfun.waterFactory_6.Ability = float.Parse(ModelLives.textBox2.Text);

                    ctrlfun.waterFactory_7.RealSpeed = float.Parse(ModelLives.textBox6.Text);
                    ctrlfun.waterFactory_7.Pool = float.Parse(ModelLives.textBox4.Text);
                    ctrlfun.waterFactory_7.Ability = float.Parse(ModelLives.textBox5.Text);

                    ctrlfun.waterFactory_2.Ability = float.Parse(ModelLives.textBox23.Text);
                    ctrlfun.waterFactory_2.RealSpeed = float.Parse(ModelLives.textBox22.Text);
                    ctrlfun.waterFactory_2.RideState[0] = ModelLives.checkBox28.Checked;
                    ctrlfun.waterFactory_2.RideState[1] = ModelLives.checkBox27.Checked;
                    ctrlfun.waterFactory_2.RideState[2] = ModelLives.checkBox26.Checked;
                    ctrlfun.waterFactory_2.RideState[3] = ModelLives.checkBox25.Checked;
                    ctrlfun.waterFactory_2.RideState[4] = ModelLives.checkBox24.Checked;
                    ctrlfun.waterFactory_2.RideState[5] = ModelLives.checkBox23.Checked;
                    ctrlfun.waterFactory_2.RideState[6] = ModelLives.checkBox22.Checked;
                    ctrlfun.waterFactory_2.RideState[7] = ModelLives.checkBox21.Checked;
                    ctrlfun.waterFactory_2.RideState[8] = ModelLives.checkBox20.Checked;
                    ctrlfun.waterFactory_2.RideState[9] = ModelLives.checkBox19.Checked;

                    ctrlfun.waterFactory_5.Ability = float.Parse(ModelLives.textBox23.Text);
                    ctrlfun.waterFactory_5.RealSpeed = float.Parse(ModelLives.textBox22.Text);
                    ctrlfun.waterFactory_5.RideState[0] = ModelLives.checkBox40.Checked;
                    ctrlfun.waterFactory_5.RideState[1] = ModelLives.checkBox39.Checked;
                    ctrlfun.waterFactory_5.RideState[2] = ModelLives.checkBox38.Checked;
                    ctrlfun.waterFactory_5.RideState[3] = ModelLives.checkBox37.Checked;
                    ctrlfun.waterFactory_5.RideState[4] = ModelLives.checkBox36.Checked;
                    ctrlfun.waterFactory_5.RideState[5] = ModelLives.checkBox35.Checked;
                    ctrlfun.waterFactory_5.RideState[6] = ModelLives.checkBox34.Checked;
                    ctrlfun.waterFactory_5.RideState[7] = ModelLives.checkBox33.Checked;
                    ctrlfun.waterFactory_5.RideState[8] = ModelLives.checkBox32.Checked;
                    ctrlfun.waterFactory_5.RideState[9] = ModelLives.checkBox31.Checked;
                    ctrlfun.waterFactory_5.RideState[10] = ModelLives.checkBox30.Checked;

                    ctrlfun.tagNameEntity.Dispacth = (DispacthMod)int.Parse(ModelLives.textBox73.Text);
                    ctrlfun.tagNameEntity.TrendOfWater = (Trend)int.Parse(ModelLives.textBox74.Text);
                    ctrlfun.tagNameEntity.UseWater_17 = float.Parse(ModelLives.textBox77.Text);
                    ctrlfun.tagNameEntity.ZhanLanGuan = float.Parse(ModelLives.textBox76.Text);
                    ctrlfun.tagNameEntity.ZhengFuJie = float.Parse(ModelLives.textBox75.Text);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.StackTrace);
                }

            }
           var result= ctrlfun.Dispatch();
            //MessageBox.Show(result.CmdoutText+CtrlFun.StepRecord.Count.ToString());
          
            if (result != null)
            textBox1.Text = result.CmdoutText;
            // textBox2.Text = TagNameEntity.Step.ToString();
            if (CtrlFun.cmd2 != null)
                textBox2.Text = CtrlFun.cmd2.CmdoutText;

            resultout = result;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {            
            Getcmd = checkBox1.Checked;
            //checkBox2.Checked = !checkBox1.Checked;
            TagNameEntity.Step = 0;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            Stepin = checkBox2.Checked;
            TagNameEntity.Step = 0;
            //checkBox1.Checked = !checkBox2.Checked;    
            
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            // this.SendToBack();
            this.WindowState = FormWindowState.Minimized;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string sqlstr = $@"insert into K_RePreSchedule (Command,TagName,value1,value2,PredureDate,IsDeleted,Status)
 Values('{textBox4.Text}','{textBox3.Text}',{textBox5.Text},{textBox6.Text},getdate(),0,0)";
            string cntstr = $@"Data Source=192.168.0.86;Initial Catalog=Tabwater_dev_new;User ID=sa;Password=abc123!@#";
            SqlConnection sqlcnt = new SqlConnection(cntstr);
            sqlcnt.Open();
            SqlCommand sqlCommand = new SqlCommand(sqlstr, sqlcnt);
            sqlCommand.ExecuteNonQuery();
            sqlcnt.Close();
            panel1.Visible = false;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox3.Text = resultout.TagName;
            textBox4.Text = resultout.Cmd;

            textBox5.Text = resultout.Value1.ToString();
            textBox6.Text = resultout.Value2.ToString();
            panel1.Visible = true;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            // CtrlFun.cmd2.TagName;
                textBox3.Text = CtrlFun.cmd2.TagName;
                textBox4.Text = CtrlFun.cmd2.Cmd;

                textBox5.Text = CtrlFun.cmd2.Value1.ToString();
                textBox6.Text = CtrlFun.cmd2.Value2.ToString();
                panel1.Visible = true;
            
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
    }
}
